local UILesson = {}

function UILesson:startLesson(lessonObj, rootNode)
    local winsize = cc.Director:getInstance():getWinSize()
    local node = cc.Node:create()
    local npc = GlobalApi:createLittleLossyAniByName("guide_npc_" .. lessonObj.npc)
    local aniName = lessonObj.animation or "idle"
    npc:getAnimation():play(aniName, -1, -1)
    npc:setAnchorPoint(cc.p(0.5, 0))
    if lessonObj.npcscalex then
        npc:setScaleX(lessonObj.npcscalex)
    end

    local picNode = self:createPicture(lessonObj.picindex)
    picNode:setPosition(cc.p(winsize.width/2 + lessonObj.picpos.x, winsize.height/2 + lessonObj.picpos.y))
    picNode:setOpacity(0)
    node:addChild(picNode)

    local dialog = cc.Sprite:create("uires/ui/guide/bg_dialog.png")
    local dialogSize = dialog:getContentSize()
    dialog:setVisible(false)

    local label = cc.Label:createWithTTF(GlobalApi:getLocalStr(lessonObj.text), "font/gamefont.ttf", 20)
    label:setMaxLineWidth(250)
    label:setAnchorPoint(cc.p(0, 1))
    label:setTextColor(COLOR_TYPE.BLACK)
    label:enableOutline(cc.c4b(255, 255, 255, 255), 1)
    -- 表情
    local emoticonSp = cc.Sprite:create("uires/ui/guide/emoticon_".. lessonObj.emoticon .. ".png")
    -- 提示文字
    local label2 = cc.Label:createWithTTF(GlobalApi:getLocalStr("CLICK_ANY_POS_CONTINUE"), "font/gamefont.ttf", 14)
    label2:setTextColor(cc.c4b(68,68,68, 255))
    node:addChild(npc)
    dialog:addChild(label)
    dialog:addChild(emoticonSp)
    dialog:addChild(label2)
    node:addChild(dialog)

    local act
    local startPos
    local scaleX
    local function showTalk()
        self.clickFlag = false
        dialog:setVisible(true)
        dialog:setScale(0)
        dialog:runAction(cc.ScaleTo:create(0.1, scaleX, 1))
    end
    local npcPosX = winsize.width/2 + lessonObj.npcpos.x
    local npcPosY = winsize.height/2 + lessonObj.npcpos.y
    if lessonObj.npcpos.x < 0 then
        startPos = cc.p(-100, npcPosY)
        npc:setPosition(startPos)
        act = cc.Sequence:create(cc.MoveBy:create(0.4, cc.p(npcPosX + 100, 0)), cc.CallFunc:create(showTalk))
        scaleX = -1
        dialog:setScaleX(-1)
        label:setScaleX(-1)
        emoticonSp:setScaleX(-1)
        label2:setScaleX(-1)
        dialog:setAnchorPoint(cc.p(1, 0))
        dialog:setPosition(cc.p(npcPosX + 100, npcPosY + 150))
        label:setPosition(cc.p(dialogSize.width - 60, dialogSize.height - 20))
        emoticonSp:setPosition(cc.p(dialogSize.width/2 - 15, 70))
        label2:setPosition(cc.p(dialogSize.width/2 - 15, 20))
    else
        startPos = cc.p(winsize.width + 100, npcPosY)
        npc:setPosition(startPos)
        act = cc.Sequence:create(cc.MoveBy:create(0.4, cc.p(npcPosX - winsize.width - 100, 0)), cc.CallFunc:create(showTalk))
        scaleX = 1
        dialog:setAnchorPoint(cc.p(1, 0))
        dialog:setPosition(cc.p(npcPosX - 110, npcPosY + 150))
        label:setPosition(cc.p(30, dialogSize.height - 20))
        emoticonSp:setPosition(cc.p(dialogSize.width/2 - 18, 70))
        label2:setPosition(cc.p(dialogSize.width/2 - 18, 20))
    end
    rootNode:addChild(node)
    npc:runAction(act)
    picNode:runAction(cc.FadeIn:create(0.4))
    self.dialog = dialog
    self.npc = npc
    self.picNode = picNode
    self.clickFlag = true
    self.startPos = startPos
end

function UILesson:createPicture(index)
    local node = cc.CSLoader:createNode("csb/warcollege_picture_" .. index .. ".csb")
    if index == 1 then
        local image = node:getChildByName("bg")
        local label1 = image:getChildByName("text_1")
        local label2 = image:getChildByName("text_2")
        local label3 = image:getChildByName("text_3")
        local label4 = image:getChildByName("text_4")
        local label5 = image:getChildByName("text_5")
        local label6 = image:getChildByName("text_6")
        local label7 = image:getChildByName("text_7")
        label1:setString(GlobalApi:getLocalStr("SOLDIER_TYPE_1"))
        label2:setString(GlobalApi:getLocalStr("STR_ATTACK_MELEE"))
        label3:setString(GlobalApi:getLocalStr("STR_ATTACK_SPEED"))
        label4:setString(GlobalApi:getLocalStr("SOLDIER_SPEED_TYPE_2"))
        label5:setString(GlobalApi:getLocalStr("STR_MOVE_SPEED"))
        label6:setString(GlobalApi:getLocalStr("SOLDIER_SPEED_TYPE_2"))
        label7:setString(GlobalApi:getLocalStr("STR_SEARCH_AI_1"))
        local richText1 = xx.RichText:create()
        richText1:setCascadeOpacityEnabled(true)
        richText1:setContentSize(cc.size(272, 40))
        local re11 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_4"), 21, cc.c4b(110, 71, 48, 255))
        re11:setFont("font/gamefont.ttf")
        re11:setStrokeSize(0)
        re11:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re12 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_5"), 21, COLOR_TYPE.RED)
        re12:setFont("font/gamefont.ttf")
        re12:setStrokeSize(0)
        re12:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re13 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_6"), 21, cc.c4b(110, 71, 48, 255))
        re13:setFont("font/gamefont.ttf")
        re13:setStrokeSize(0)
        re13:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        richText1:addElement(re11)
        richText1:addElement(re12)
        richText1:addElement(re13)
        richText1:setPosition(cc.p(10, 96))
        richText1:setAlignment('left')
        richText1:setAnchorPoint(cc.p(0, 1))
        image:addChild(richText1)

        local richText2 = xx.RichText:create()
        richText2:setCascadeOpacityEnabled(true)
        richText2:setContentSize(cc.size(272, 40))
        local re21 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_7"), 21, cc.c4b(110, 71, 48, 255))
        re21:setFont("font/gamefont.ttf")
        re21:setStrokeSize(0)
        re21:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re22 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_8"), 21, COLOR_TYPE.RED)
        re22:setFont("font/gamefont.ttf")
        re22:setStrokeSize(0)
        re22:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re23 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_6"), 21, cc.c4b(110, 71, 48, 255))
        re23:setFont("font/gamefont.ttf")
        re23:setStrokeSize(0)
        re23:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        richText2:addElement(re21)
        richText2:addElement(re22)
        richText2:addElement(re23)
        richText2:setPosition(cc.p(282, 96))
        richText2:setAlignment('left')
        richText2:setAnchorPoint(cc.p(0, 1))
        image:addChild(richText2)
    elseif index == 2 then
        local image = node:getChildByName("bg")
        local label1 = image:getChildByName("text_1")
        local label2 = image:getChildByName("text_2")
        local label3 = image:getChildByName("text_3")
        local label4 = image:getChildByName("text_4")
        local label5 = image:getChildByName("text_5")
        local label6 = image:getChildByName("text_6")
        local label7 = image:getChildByName("text_7")
        label1:setString(GlobalApi:getLocalStr("SOLDIER_TYPE_2"))
        label2:setString(GlobalApi:getLocalStr("STR_ATTACK_REMOTE"))
        label3:setString(GlobalApi:getLocalStr("STR_ATTACK_SPEED"))
        label4:setString(GlobalApi:getLocalStr("SOLDIER_SPEED_TYPE_1"))
        label5:setString(GlobalApi:getLocalStr("STR_MOVE_SPEED"))
        label6:setString(GlobalApi:getLocalStr("SOLDIER_SPEED_TYPE_3"))
        label7:setString(GlobalApi:getLocalStr("STR_SEARCH_AI_2"))

        local richText3 = xx.RichText:create()
        richText3:setCascadeOpacityEnabled(true)
        richText3:setContentSize(cc.size(272, 40))
        local re31 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_9"), 21, cc.c4b(110, 71, 48, 255))
        re31:setFont("font/gamefont.ttf")
        re31:setStrokeSize(0)
        re31:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re32 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_10"), 21, COLOR_TYPE.RED)
        re32:setFont("font/gamefont.ttf")
        re32:setStrokeSize(0)
        re32:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re33 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_6"), 21, cc.c4b(110, 71, 48, 255))
        re33:setFont("font/gamefont.ttf")
        re33:setStrokeSize(0)
        re33:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        richText3:addElement(re31)
        richText3:addElement(re32)
        richText3:addElement(re33)
        richText3:setPosition(cc.p(10, 106))
        richText3:setAlignment('left')
        richText3:setAnchorPoint(cc.p(0, 1))
        image:addChild(richText3)

        local richText4 = xx.RichText:create()
        richText4:setCascadeOpacityEnabled(true)
        richText4:setContentSize(cc.size(272, 40))
        local re41 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_11"), 21, cc.c4b(110, 71, 48, 255))
        re41:setFont("font/gamefont.ttf")
        re41:setStrokeSize(0)
        re41:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re42 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_12"), 21, COLOR_TYPE.RED)
        re42:setFont("font/gamefont.ttf")
        re42:setStrokeSize(0)
        re42:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re43 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_6"), 21, cc.c4b(110, 71, 48, 255))
        re43:setFont("font/gamefont.ttf")
        re43:setStrokeSize(0)
        re43:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        richText4:addElement(re41)
        richText4:addElement(re42)
        richText4:addElement(re43)
        richText4:setPosition(cc.p(282, 106))
        richText4:setAlignment('left')
        richText4:setAnchorPoint(cc.p(0, 1))
        image:addChild(richText4)
    elseif index == 3 then
        local image = node:getChildByName("bg")
        local label1 = image:getChildByName("text_1")
        local label2 = image:getChildByName("text_2")
        local label3 = image:getChildByName("text_3")
        local label4 = image:getChildByName("text_4")
        local label5 = image:getChildByName("text_5")
        local label6 = image:getChildByName("text_6")
        local label7 = image:getChildByName("text_7")
        label1:setString(GlobalApi:getLocalStr("SOLDIER_TYPE_3"))
        label2:setString(GlobalApi:getLocalStr("STR_ATTACK_MELEE"))
        label3:setString(GlobalApi:getLocalStr("STR_ATTACK_SPEED"))
        label4:setString(GlobalApi:getLocalStr("SOLDIER_SPEED_TYPE_2"))
        label5:setString(GlobalApi:getLocalStr("STR_MOVE_SPEED"))
        label6:setString(GlobalApi:getLocalStr("SOLDIER_SPEED_TYPE_1"))
        label7:setString(GlobalApi:getLocalStr("STR_SEARCH_AI_2"))

        local richText5 = xx.RichText:create()
        richText5:setCascadeOpacityEnabled(true)
        richText5:setContentSize(cc.size(272, 40))
        local re51 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_4"), 21, cc.c4b(110, 71, 48, 255))
        re51:setFont("font/gamefont.ttf")
        re51:setStrokeSize(0)
        re51:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re52 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_5"), 21, COLOR_TYPE.RED)
        re52:setFont("font/gamefont.ttf")
        re52:setStrokeSize(0)
        re52:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re53 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_6"), 21, cc.c4b(110, 71, 48, 255))
        re53:setFont("font/gamefont.ttf")
        re53:setStrokeSize(0)
        re53:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        richText5:addElement(re51)
        richText5:addElement(re52)
        richText5:addElement(re53)
        richText5:setPosition(cc.p(10, 96))
        richText5:setAlignment('left')
        richText5:setAnchorPoint(cc.p(0, 1))
        image:addChild(richText5)

        local richText6 = xx.RichText:create()
        richText6:setCascadeOpacityEnabled(true)
        richText6:setContentSize(cc.size(272, 40))
        local re61 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_13"), 21, cc.c4b(110, 71, 48, 255))
        re61:setFont("font/gamefont.ttf")
        re61:setStrokeSize(0)
        re61:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re62 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_14"), 21, COLOR_TYPE.RED)
        re62:setFont("font/gamefont.ttf")
        re62:setStrokeSize(0)
        re62:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        local re63 = xx.RichTextLabel:create(GlobalApi:getLocalStr("STR_SEARCH_AI_6"), 21, cc.c4b(110, 71, 48, 255))
        re63:setFont("font/gamefont.ttf")
        re63:setStrokeSize(0)
        re63:setShadow(COLOR_TYPE.WHITE, cc.size(0, -1))
        richText6:addElement(re61)
        richText6:addElement(re62)
        richText6:addElement(re63)
        richText6:setPosition(cc.p(282, 96))
        richText6:setAlignment('left')
        richText6:setAnchorPoint(cc.p(0, 1))
        image:addChild(richText6)
    end
    return node
end

function UILesson:onClick()
    if not self.clickFlag then
        self.clickFlag = true
        self.dialog:setVisible(false)
        self.picNode:runAction(cc.FadeOut:create(0.4))
        self.npc:runAction(cc.Sequence:create(cc.MoveTo:create(0.4, cc.p(self.startPos)), cc.CallFunc:create(function ()
            self:finish()
        end)))
    end
end

function UILesson:finish()
    self.dialog = nil
    self.npc = nil
    self.clickFlag = nil
    self.startPos = nil
    self.picNode = nil
	WarCollegeMgr:nextStep()
end

return UILesson